package com.im.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SimpleQuery;
import com.im.service.common.Result;
import com.im.service.domain.entity.ImFriend;
import com.im.service.domain.entity.ImUser;
import com.im.service.mapper.ImFriendMapper;
import com.im.service.mapper.ImUserMapper;
import com.im.service.service.ImFriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 好友列表 服务实现类
 * </p>
 *
 * @author 刘志强
 * @since 2024-07-03
 */
@Service
public class ImFriendServiceImpl extends ServiceImpl<ImFriendMapper, ImFriend> implements ImFriendService {

    @Autowired
    private ImUserMapper userMapper;

    @Override
    public List<ImUser> getFriendsByUserId(Long userId) {
        List<Long> friendUserIdList = SimpleQuery.list(
                Wrappers.lambdaQuery(ImFriend.class)
                        .eq(ImFriend::getUserId, userId),
                ImFriend::getFriendUserId
        );
        if (CollectionUtil.isNotEmpty(friendUserIdList)) {
            List<ImUser> friends = userMapper.selectList(
                    Wrappers.lambdaQuery(ImUser.class)
                            .in(ImUser::getId, friendUserIdList)
            );
            return friends;
        }
        return null;


    }

    @Override
    public Result<List<ImUser>> myFriend(Long userId) {
        return Result.okData(getFriendsByUserId(userId));
    }
}
